package com.nbufe.service.impl;

import com.alibaba.fastjson.JSON;
import com.nbufe.domain.entity.*;
import com.nbufe.domain.entity.Process;
import com.nbufe.domain.vo.base.EmissionOriginBaseVo;
import com.nbufe.mapper.*;
import com.nbufe.service.BaseService;
import com.nbufe.service.CarbonStatisticsService;
import com.nbufe.service.EmissionFactorBankService;
import com.nbufe.service.EmissionOriginService;
import com.nbufe.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author luzhiheng
 * @Date 2023/12/26 15:08
 */
@Service
public class BaseServiceImpl implements BaseService {

    @Resource
    private ProcessBaseMapper processBaseMapper;

    @Resource
    private ProcessMapper processMapper;

    @Resource
    private EmissionOriginService emissionOriginService;

    @Resource
    private EmissionOriginBaseMapper emissionOriginBaseMapper;

    @Resource
    private EmissionFactorBankService emissionFactorBankService;

    @Resource
    private EmissionFactorBankBaseMapper emissionFactorBankBaseMapper;

    @Resource
    private CarbonStatisticsService carbonStatisticsService;

    @Override
    public void initProcessData(Long tenantId, Map<Long, List<EmissionOriginBaseVo>> map) {
        List<ProcessBase> processBaseList = processBaseMapper.selectList(null);
        processBaseList.forEach(processBase -> {
            Process process = BeanCopyUtils.copyBean(processBase, Process.class);
            process.setTenantId(tenantId);
            List<Long> idList = JSON.parseArray(processBase.getEmissionOrigin(), Long.class);
            List<Long> newIdList = new ArrayList<>();
            idList.forEach(id -> {
                Long newId = map.get(id).get(0).getId();
                newIdList.add(newId);
            });
            process.setEmissionOrigin(JSON.toJSONString(newIdList));
            process.setId(null);
            processMapper.insert(process);
        });
    }

    @Override
    public List<EmissionOriginBaseVo> initEmissionOriginData(Long tenantId) {
        List<EmissionOriginBase> emissionOriginBaseList = emissionOriginBaseMapper.selectList(null);
        List<EmissionOrigin> emissionOriginList = BeanCopyUtils.copyBeanList(emissionOriginBaseList, EmissionOrigin.class);

        List<EmissionOriginBaseVo> emissionOriginBaseVoList = new ArrayList<>();
        emissionOriginList.forEach(emissionOrigin -> {
            EmissionOriginBaseVo emissionOriginBaseVo = BeanCopyUtils.copyBean(emissionOrigin, EmissionOriginBaseVo.class);
            emissionOriginBaseVo.setBaseId(emissionOrigin.getId());

            emissionOrigin.setTenantId(tenantId);
            emissionOrigin.setId(null);
            emissionOriginService.save(emissionOrigin);

            emissionOriginBaseVo.setId(emissionOrigin.getId());
            emissionOriginBaseVoList.add(emissionOriginBaseVo);
        });

        return emissionOriginBaseVoList;
    }

    @Override
    public void initEmissionFactorData(Long tenantId) {
        List<EmissionFactorBankBase> emissionFactorBankBaseList = emissionFactorBankBaseMapper.selectList(null);
        List<EmissionFactorBank> emissionFactorBankList = BeanCopyUtils.copyBeanList(emissionFactorBankBaseList, EmissionFactorBank.class);
        emissionFactorBankList = emissionFactorBankList.stream()
                .peek(emissionFactorBank -> {
                    emissionFactorBank.setTenantId(tenantId);
                    emissionFactorBank.setId(null);
                })
                .collect(Collectors.toList());
        for (EmissionFactorBank emissionFactorBank : emissionFactorBankList) {
            emissionFactorBankService.save(emissionFactorBank);
        }
    }

    @Transactional
    @Override
    public void initTenantData(Long tenantId) {
        // 1.初始化排放源数据
        List<EmissionOriginBaseVo> emissionOriginList = initEmissionOriginData(tenantId);
        Map<Long, List<EmissionOriginBaseVo>> map = emissionOriginList.stream().collect(Collectors.groupingBy(EmissionOriginBaseVo::getBaseId));
        // 2.初始化工序基础数据
        initProcessData(tenantId, map);
        // 3.初始碳排放因子库
        initEmissionFactorData(tenantId);
        // 4.初始化碳交易数据
        initCarbonTradeData(tenantId);
    }

    private void initCarbonTradeData(Long tenantId) {
        CarbonStatistics statistics = new CarbonStatistics();
        statistics.setCarbonEmission((double) 0);
        statistics.setDelFlag(0);
        statistics.setCreateTime(new Date());
        statistics.setUsedAssert((double) 0);
        statistics.setUsedCcer((double) 0);
        statistics.setUsedQuota((double) 0);
        statistics.setTotalCcer((double) 0);
        statistics.setTotalQuota((double) 0);
        statistics.setTenantId(tenantId);
        carbonStatisticsService.save(statistics);
    }
}
